import { Button, Tooltip } from 'antd';
import { Fragment } from 'react';
import { removeDateHmsStr, tooltipLength } from '@/utils';
import { BZWC_STATE, CQXG_STATE, DB_STATE, LB_STATE, LRKPB_STATE, getSorter } from '../common/Format';
import { ManageCommonAPI } from '@/pages/type';
import { EditOutlined, AuditOutlined, RedoOutlined, SoundOutlined } from '@ant-design/icons';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';
import { SchemaManageAPI } from '@/components/SchemaComponent/SchemaManage/type';
import { ColumnsRenderFun, DictionaryRenderFun, formatDecimal, procurementCodeCheck } from '../../Acommon';
import pageStyles from '@/common/pageLayout.less';
import MyIcon from '@/components/MyIcon';
import { findValueFun } from '@/common';
import { KYFW_Type, KYWZ_Type, PURCHASE_Type, ZNGL_Type } from '../../ApplyManage/common/Format';
/** 操作按钮的显示 */
const Operation = (curRes: Record<string, any>, operateFuns: Record<string, any>, record: Record<string, any>) => {
  let operationArray: any = [];

  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );

  if (procurementCodeCheck(record?.procureReal)) {
    return [Disable];
  } else {
    /** 更多按钮配置 */
    const otherBtnConfig: ManageCommonAPI.TableMoreBtnConfigApi[] = [
      {
        icon: <AuditOutlined />,
        btnName: '公告',
        clickFnc: operateFuns,
        clickType: 'onEdit',
        isVisible: record?.state == BZWC_STATE() && curRes?.['/purchaseSourceController/addBid'],
      },
      {
        icon: <SoundOutlined />,
        btnName: '澄清',
        clickFnc: operateFuns,
        clickType: 'onClarify',
        isVisible: record?.state == CQXG_STATE() && curRes?.['/purchaseSourceClarifyController/add'],
      },
      {
        icon: <EditOutlined />,
        btnName: '开标',
        clickFnc: operateFuns,
        clickType: 'onSupplier',
        isVisible: record?.state == LRKPB_STATE() && curRes?.['/purchaseSourceSupplierController/add'],
      },
      {
        icon: <EditOutlined />,
        btnName: '定标',
        clickFnc: operateFuns,
        clickType: 'onCalibrate',
        isVisible: record?.state == DB_STATE() && curRes?.['/purchaseSourceCalibrateController/addSourceCalibrate'],
      },
      {
        icon: <AuditOutlined />,
        btnName: '公告',
        clickFnc: operateFuns,
        clickType: 'onAnnoun',
        isVisible:
          record?.state == LB_STATE() &&
          (record?.lockFlag == 1 || record?.lockFlag == 2 || record?.lockFlag == 3) &&
          curRes?.['/purchaseSourceController/failureBid'],
      },
      {
        icon: <RedoOutlined />,
        btnName: '变更',
        clickFnc: operateFuns,
        clickType: 'onChangeMethod',
        isVisible:
          record?.state == LB_STATE() &&
          (record?.lockFlag == 1 || record?.lockFlag == 2 || record?.lockFlag == 3) &&
          curRes?.['/purchaseSourceController/failureChange'],
      },
    ];

    const ohterBtn = tableMoreBtnConfig(otherBtnConfig, record);
    operationArray = ohterBtn;
    operationArray = operationArray?.filter((itemBtn: any) => itemBtn);
    return operationArray && operationArray?.length > 0 ? operationArray : [Disable];
  }
};

/**
 * @getApplyManageTableColumns
 * @description 寻源管理数据表格信息展示和操作
 */
const getSourceManageTableColumns = (operation: any, curRes): Record<string, any>[] => {
  return [
    {
      title: '单位名称', // 原【申请部门】
      dataIndex: 'ownCompanyName', // own
      key: 'ownCompanyName',
      visible: true,
      fixed: 'left',
      sorter: (a: any, b: any) => getSorter(a?.ownCompanyName, b?.ownCompanyName),
      width: 250,
      render: (text: string, record) => {
        return (
          <Tooltip title={tooltipLength(text, 50)}>
            <span className={pageStyles['over-flow-hidden']} style={{ width: '80%' }}>
              {record?.pID && <MyIcon type="icon-dian" isTheme={false} />}
              {text == null || text == undefined || text == '' ? '--' : text}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '承办部门/团队', // 原【申请部门】
      dataIndex: 'ownDepartmentName', // own
      key: 'ownDepartmentName',
      visible: true,
      fixed: 'left',
      sorter: (a: any, b: any) => getSorter(a?.ownDepartmentName, b?.ownDepartmentName),
      width: 250,
      render: (text: string, record) => {
        return (
          <Tooltip title={tooltipLength(text, 50)}>
            <span className={pageStyles['over-flow-hidden']} style={{ width: '80%' }}>
              {record?.pID && <MyIcon type="icon-dian" isTheme={false} />}
              {text == null || text == undefined || text == '' ? '--' : text}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '采购项目名称',
      dataIndex: 'applyTitle',
      key: 'applyTitle',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.applyTitle, b?.applyTitle),
      width: 220,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '标包名称',
      dataIndex: 'packName',
      key: 'packName',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.packName, b?.packName),
      width: 250,
      render: (text: string, record: Record<string, any>) => {
        if (procurementCodeCheck(record?.procureReal)) {
          // return ColumnsRenderFun({ text: `${text}(${record?.packCode})`, isTooltip: true });
          return (
            <a
              onClick={() => {
                operation?.onShowDrawer(record);
              }}
              style={{ color: '100%' }}
            >
              <Tooltip title={tooltipLength(`${text}(${record?.packCode})`, 50)}>
                <span className={pageStyles['over-flow-hidden']}>{`${text}(${record?.packCode})` || '--'}</span>
              </Tooltip>
            </a>
          );
        }
        return (
          <a
            onClick={() => {
              if (curRes?.['/purchaseSourceController/getBidById']) {
                operation?.onViewPage(record);
              }
            }}
            style={{ color: '100%' }}
          >
            <Tooltip title={tooltipLength(`${text}(${record?.packCode})`, 50)}>
              <span className={pageStyles['over-flow-hidden']}>{`${text}(${record?.packCode})` || '--'}</span>
            </Tooltip>
          </a>
        );
      },
    },
    {
      title: '采购类别',
      dataIndex: 'purchCategory',
      key: 'purchCategory',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.purchCategory, b?.purchCategory),
      width: 220,
      render: (text: string) => {
        return <span>{findValueFun(PURCHASE_Type, text)}</span>;
      },
    },
    {
      title: '采购内容',
      dataIndex: 'purchContent',
      key: 'purchContent',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.purchContent, b?.purchContent),
      width: 220,
      render: (text: string, record) => {
        return (
          <span>
            {record?.purchCategory == 0
              ? findValueFun(KYWZ_Type, text)
              : record?.purchCategory == 1
              ? findValueFun(KYFW_Type, text)
              : record?.purchCategory == 2
              ? findValueFun(ZNGL_Type, text)
              : '--'}
          </span>
        );
      },
    },
    {
      title: '其他内容',
      dataIndex: 'otherContent',
      key: 'otherContent',
      visible: false,
      sorter: (a: any, b: any) => getSorter(a?.otherContent, b?.otherContent),
      width: 220,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '标的来源',
      dataIndex: 'supplyWay',
      key: 'supplyWay',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.supplyWay, b?.supplyWay),
      width: 140,
      render: (text: number) => {
        return text == 0 ? '月度计划' : text == 1 ? '零星采购' : text == 2 ? '实验平台' : '--';
      },
    },
    {
      title: '采购组织类型',
      dataIndex: 'procureOrgName',
      key: 'procureOrgName',
      sorter: (a: any, b: any) => getSorter(a?.procureOrgName, b?.procureOrgName),
      visible: true,
      width: 180,
      render: (text: string) => DictionaryRenderFun({ text }),
    },
    {
      title: '拟采用采购方式',
      dataIndex: 'procureRealName',
      key: 'procureRealName',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.procureRealName, b?.procureRealName),
      width: 180,
      render: (text: string) => DictionaryRenderFun({ text }),
    },
    {
      title: '状态',
      dataIndex: 'stateName',
      key: 'stateName',
      sorter: (a: any, b: any) => getSorter(a?.state, b?.state),
      visible: true,
      width: 150,
      render: (text: string) => DictionaryRenderFun({ text, isState: true }),
    },
    {
      title: '预算金额（元）',
      dataIndex: 'expectSum',
      key: 'expectSum',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.expectSum, b?.expectSum),
      width: 180,
      render: (text: string) => <span>{formatDecimal(text)}</span>,
    },
    {
      title: '申请时间',
      dataIndex: 'ownTime',
      key: 'ownTime',
      sorter: (a: any, b: any) => getSorter(a?.ownTime, b?.ownTime),
      width: 160,
      visible: true,
      render: (text: string) => {
        if (text) {
          text = text?.split(' ')?.[0];
        } else {
          text = '--';
        }
        return ColumnsRenderFun({ text });
      },
    },
    {
      title: '申请时间',
      dataIndex: 'ownTime',
      key: 'ownTime',
      sorter: (a: any, b: any) => getSorter(a?.ownTime, b?.ownTime),
      width: 160,
      visible: true,
      render: (text) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '申请人公司',
      dataIndex: 'ownCompanyName',
      key: 'ownCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.ownCompanyName > b?.ownCompanyName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: '申请人部门',
      dataIndex: 'ownDepartmentName',
      key: 'ownDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.ownDepartmentName > b?.ownDepartmentName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: '申请人岗位',
      dataIndex: 'ownPositionName',
      key: 'ownPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.ownPositionName > b?.ownPositionName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: '申请人',
      dataIndex: 'ownerName',
      key: 'ownerName',
      sorter: (a: any, b: any) => getSorter(a?.ownerName, b?.ownerName),
      width: 120,
      visible: true,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: '修改时间',
      dataIndex: 'modifyTime',
      key: 'modifyTime',
      visible: false,
      width: 160,
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '修改人公司',
      dataIndex: 'modifyCompanyName',
      key: 'modifyCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyCompanyName > b?.modifyCompanyName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: '修改人部门',
      dataIndex: 'modifyDepartmentName',
      key: 'modifyDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyDepartmentName > b?.modifyDepartmentName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: '修改人岗位',
      dataIndex: 'modifyPositionName',
      key: 'modifyPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyPositionName > b?.modifyPositionName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: '修改人',
      dataIndex: 'modifyerName',
      key: 'modifyerName',
      width: 120,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: '作废类型', //数据字典
      dataIndex: 'cancelTypeName',
      key: 'cancelTypeName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelTypeName > b?.cancelTypeName ? 1 : -1),
      render: (text: string) => DictionaryRenderFun({ text }),
    },
    {
      title: '作废原因', //数据字典
      dataIndex: 'cancelReason',
      key: 'cancelReason',
      visible: false,
      width: 180,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelReason > b?.cancelReason ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: '作废时间',
      dataIndex: 'cancelTime',
      key: 'cancelTime',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelTime > b?.cancelTime ? 1 : -1),
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '作废人公司',
      dataIndex: 'cancelCompanyName',
      key: 'cancelCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelCompanyName > b?.cancelCompanyName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: '作废人部门',
      dataIndex: 'cancelDepartmentName',
      key: 'cancelDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelDepartmentName > b?.cancelDepartmentName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: '作废人岗位',
      dataIndex: 'cancelPositionName',
      key: 'cancelPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelPositionName > b?.cancelPositionName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: '作废人',
      dataIndex: 'cancelerName',
      key: 'cancelerName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelerName > b?.cancelerName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 180,
      visible: true,
      unDrag: true,
      align: 'left',
      fixed: 'right',
      render: (text: string | null, record: Record<string, any>) => {
        return <div style={{ display: 'flex' }}>{Operation(curRes, operation, record)}</div>;
      },
    },
  ];
};

/**
 * @getApplyManageTableHeaderActions
 * @description 寻源管理表格顶部自定义按钮
 * @returns SchemaManageAPI.IActionBtnList[]
 */
const getSourceManageTableHeaderActions = (operation: any): SchemaManageAPI.IActionBtnList[] => {
  return [];
  // const { onOpen } = operation;
  // return [
  //   {
  //     title: '采购申请',
  //     visible: true,
  //     type: 'primary',
  //     icon: <PlusOutlined />,
  //     onClick: () => {
  //       onOpen();
  //     },
  //   }
  // ];
};
export { getSourceManageTableColumns, getSourceManageTableHeaderActions };

// {
//   title: '联系人',
//   dataIndex: 'ownerName',
//   key: 'ownerName',
//   sorter: (a: any, b: any) => getSorter(a?.ownerName, b?.ownerName),
//   visible: true,
//   width: 150,
//   render: (text: string) => ColumnsRenderFun({ text }),
// },
// {
//   title: '联系电话',
//   dataIndex: 'ownPhone',
//   key: 'ownPhone',
//   sorter: (a: any, b: any) => getSorter(a?.ownPhone, b?.ownPhone),
//   visible: true,
//   width: 150,
//   render: (text: string) => ColumnsRenderFun({ text }),
// },

// {
//   title: '预计采购时间',
//   dataIndex: 'expectTime',
//   key: 'expectTime',
//   width: 160,
//   visible: true,
//   render: (text) => <span>{removeDateHmsStr(text)}</span>,
// },
